package com.minimoment.model.service.impl;

import com.minimoment.base.BaseSupportImpl;
import com.minimoment.model.domain.Area;
import com.minimoment.model.service.AreaService;
import com.util.EncodeOrder;
import org.hibernate.Hibernate;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.StringType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by etund on 2014/11/3.
 */
@Service
@Transactional
@SuppressWarnings("all")
public class AreaServiceImpl extends BaseSupportImpl<Area> implements AreaService {

    private List<Area> areas = new ArrayList<Area>();

    public List<Area> getTheProvince(){
        areas = this.getSession().createQuery("from Area a where a.id like ?").setParameter(0,"AA__").list();
        return areas;
    }


    public List<Area> getTheCity(String id){
        areas = this.getSession().createQuery("from Area a where a.id like ?").setParameter(0,id+"__").list();
        return areas;
    }

    /**
     * 现在hibernate 版本升级了，就报错了，现在是下面的代替方法：
     Java代码  收藏代码
     // 用这里面的警醒代替
     StandardBasicTypes.CHARACTER_ARRAY
     StringType.INSTANCE
     * @returns
     */
    public List<Area> getAllAreas(){
        areas =this.getSession().createCriteria(Area.class)
                .add( Restrictions.like("id", "AA____") )
                .addOrder(EncodeOrder.asc("name"))
                .setFirstResult(359)
                .setMaxResults(120)
                .list();

//        areas = this.getSession().createQuery("from  Area a where a.id like ? order by a.pinyin ").setParameter(0,"AA%").list();
        return areas;
    }
    public List<Area> getTheAreas(){
        areas = this.getSession().createCriteria(Area.class).setMaxResults(20).list();
        return areas;
    }

    @Override
    public List<Area> getTheAreas(String str) {
        if(str.equals("a")){
            areas =this.getSession().createCriteria(Area.class)
                    .add( Restrictions.like("id", "AA____") )
                    .addOrder(EncodeOrder.asc("name"))
                    .setFirstResult(0)
                    .setMaxResults(114)
                    .list();
        }
        if(str.equals("h")){
            areas =this.getSession().createCriteria(Area.class)
                    .add( Restrictions.like("id", "AA____") )
                    .addOrder(EncodeOrder.asc("name"))
                    .setFirstResult(114)
                    .setMaxResults(123)
                    .list();
        }
        if(str.equals("m")){
            areas =this.getSession().createCriteria(Area.class)
                    .add( Restrictions.like("id", "AA____") )
                    .addOrder(EncodeOrder.asc("name"))
                    .setFirstResult(237)
                    .setMaxResults(121)
                    .list();
        }if(str.equals("w")){
            areas =this.getSession().createCriteria(Area.class)
                    .add( Restrictions.like("id", "AA____") )
                    .addOrder(EncodeOrder.asc("name"))
                    .setFirstResult(358)
                    .setMaxResults(120)
                    .list();
        }
        return areas;
    }

    public List<Area>  getTown() {
        areas = this.getSession().createCriteria(Area.class)
                .add(Restrictions.like("id", "AAAIAU__"))
                .list();
        return areas;
    }
}
